//
// Created by steven on 2017/3/7.
// Copyright (c) 2017 steven. All rights reserved.
//

#import "GMSubUserDataController.h"
#import "GMSubUserModel.h"


@implementation GMSubUserDataController

- (void)requestSubUsersWithParams:(NSDictionary *)params callback:(GMCompletionCallback)callback {
    __weak typeof(self)weakSelf = self;

    [self requestWithType:GET url:ACTION_SUBUSER params:params success:^(id object) {
        __strong typeof(weakSelf)strongSelf = weakSelf;
        NSDictionary *metaInfo = object[@"meta"][@"pagination"];
        NSArray *data = object[@"data"];
        strongSelf.subUserRecord.totalCount = [metaInfo[@"total"] integerValue];
        strongSelf.subUserRecord.currentPage = [metaInfo[@"current_page"] integerValue];
        strongSelf.subUserRecord.totalPage = [metaInfo[@"total_pages"] integerValue];

        NSMutableArray *tempArray = [[NSMutableArray alloc] init];
        [data enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            GMSubUserModel *subUserModel = [[GMSubUserModel alloc] init];
            subUserModel.id = [obj[@"id"] integerValue];
            subUserModel.name = obj[@"name"];
            subUserModel.account = obj[@"account"];
            subUserModel.headImg = obj[@"headImg"];
            subUserModel.permission = obj[@"permission"];
            subUserModel.password = obj[@"password"];
            subUserModel.status = obj[@"active"];
            subUserModel.phoneNumber = obj[@"phoneNumber"];

            [tempArray addObject:subUserModel];
        }];

        if (strongSelf.subUserRecord.currentPage == 1) {
            [strongSelf.subUserRecord.subUsers removeAllObjects];
        }

        [strongSelf.subUserRecord.subUsers addObjectsFromArray:tempArray];
        callback(nil);
    } failure:^(NSInteger statusCode, NSString *message) {
        NSString *errorString = nil;
        switch (statusCode) {
            case 400:
            case 401:
            case 403:
            case 404:
                errorString = @"获取子失败";
                break;
            case 406:
                errorString = @"无效请求";
                break;
            case 500:
                errorString = @"创建token时出错";
                break;
            case SERVER_NETWORK_OFF:
                errorString = @"服务器错误";
                break;
            case USER_NETWORK_OFF:
                errorString = @"请检查手机网络";
                break;
            default:
                errorString = UNKOWN_ERROR;
                break;
        }

        callback(message ? message : errorString);
    }];
}

// Create new sub user
- (void)createSubUserWithParams:(NSDictionary *)params callback:(GMCompletionCallback)callback {
    [self requestWithType:POST url:ACTION_ADD_SUBUSER params:params success:^(id object) {
        callback(nil);
    } failure:^(NSInteger statusCode, NSString *message) {
        NSString *errorString = nil;
        switch (statusCode) {
            case 400:
            case 401:
            case 403:
            case 500:
                errorString = @"添加失败";
                break;
            case 406:
                errorString = @"无效请求";
                break;
            case SERVER_NETWORK_OFF:
                errorString = @"服务器错误";
                break;
            case USER_NETWORK_OFF:
                errorString = @"请检查手机网络";
                break;
            default:
                errorString = UNKOWN_ERROR;
                break;
        }

        callback(message ? message : errorString);
    }];
}

// Delete sub user
- (void)deleteSubUserWithParams:(NSDictionary *)params callback:(GMCompletionCallback)callback {
    [self requestWithType:POST url:ACTION_REMOVE_SUBUSER params:params success:^(id object) {
        callback(nil);
    } failure:^(NSInteger statusCode, NSString *message) {
        NSString *errorString = nil;
        switch (statusCode) {
            case 400:
            case 401:
            case 403:
            case 500:
                errorString = @"删除失败";
                break;
            case 406:
                errorString = @"无效请求";
                break;
            case SERVER_NETWORK_OFF:
                errorString = @"服务器错误";
                break;
            case USER_NETWORK_OFF:
                errorString = @"请检查手机网络";
                break;
            default:
                errorString = UNKOWN_ERROR;
                break;
        }

        callback(message ? message : errorString);
    }];
}

// Edit subuser
- (void)updateSubUserWithParams:(NSDictionary *)params callback:(GMCompletionCallback)callback {
    __weak typeof(self)weakSelf = self;
    [self requestWithType:POST url:ACTION_UPDATE_SUBUSER params:params success:^(id object) {
        __strong typeof(weakSelf)strongSelf = weakSelf;
        strongSelf.response = object;
        callback(nil);
    } failure:^(NSInteger statusCode, NSString *message) {
        NSString *errorString = nil;
        switch (statusCode) {
            case 400:
            case 401:
            case 403:
            case 500:
                errorString = @"更新失败";
                break;
            case 406:
                errorString = @"无效请求";
                break;
            case SERVER_NETWORK_OFF:
                errorString = @"服务器错误";
                break;
            case USER_NETWORK_OFF:
                errorString = @"请检查手机网络";
                break;
            default:
                errorString = UNKOWN_ERROR;
                break;
        }

        callback(message ? message : errorString);
    }];
}

#pragma mark - sub user record
- (GMSubUserRecord *)subUserRecord {
    if (!_subUserRecord) {
        _subUserRecord = [[GMSubUserRecord alloc] init];
        _subUserRecord.subUsers = [[NSMutableArray alloc] init];
    }

    return _subUserRecord;
}
@end